import {defineStore} from 'pinia';
import {RequestAPI} from "@/utils/RequestAPI";
import {md5} from "@/utils/md5js";
import {UniStore} from "@/utils/UniStorege";


export const useLoginStore = defineStore('logService', {
  state() {
    return {
      logInfo: {
        token: ''
      }
    }
  },
  actions: {
    async login(form) {
      let api = new RequestAPI()
      try {
        let {password, username = ''} = form || {};
        let res = await api.post('login', {
          password: md5(password),
          account: username.trim(),
        });

        let {data} = res || {};
        let {
          Token = '',
          UserInfo = {},
          flag,
          customId,
          customName,
          rightList
        } = data || {};
        let isTemp = flag === 2
        UniStore.setTempUserInfo({
          isTemp,
          customId,
          customName,
          rightList
        })
        UniStore.setT(Token);
        UniStore.setUserInfo(UserInfo, {...form,rem: true})

        return res;
      } catch (err) {
        throw new Error(err);
      }
    },
    async login_old(form) {
      let api = new RequestAPI()
      try {
        let {password, username = ''} = form || {};
        let res = await api.post('zc/login', {
          password: md5(password),
          account: username.trim(),
        });
        return res;
      } catch (err) {
        throw new Error(err);
      }
    }
  }
})
